package com.hpjpw.idoapp.service;

import com.hpjpw.ido.domain.Enterprise;
import com.hpjpw.ido.domain.Worker;
import com.hpjpw.ido.service.IEnterpriseService;
import com.hpjpw.ido.service.IWorkerService;
import com.hpjpw.idoapp.domain.WorkerRegisterVo;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: RuoYi-IDo
 * @BelongsPackage: com.hpjpw.idoapp.service
 * @Author: hpj
 * @CreateTime: 2021-01-08 20:56
 * @Description: 企业员工
 */
@Service
public class IDoWorkerService {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IEnterpriseService enterpriseService;
    @Autowired
    private UserImportService userImportService;
    @Autowired
    private IWorkerService workerService;

    public AjaxResult addWorker(WorkerRegisterVo workerRegisterVo, String operName) {
        Enterprise enterprise_q = new Enterprise();
        enterprise_q.setCode(workerRegisterVo.getRegCode());
        List<Enterprise> enterpriseList = enterpriseService.selectEnterpriseList(enterprise_q);
        //检查是否存在这样的企业
        if (StringUtils.isNull(enterpriseList) || enterpriseList.isEmpty()) {
            AjaxResult.error("企业注册码错误");
        }
        Enterprise enterprise = enterpriseList.get(0);

        Worker worker_q = new Worker();
        worker_q.setNumber(workerRegisterVo.getNumber());
        worker_q.setEnterpriseId(enterprise.getId());
        List<Worker> workerList = workerService.selectWorkerList(worker_q);

        //检查是否存在这样的企业员工
        if (StringUtils.isNotNull(workerList) && !workerList.isEmpty())
            return AjaxResult.error("该职工号已被注册");

        SysUser sysUser_q = new SysUser();
        sysUser_q.setPhonenumber(workerRegisterVo.getPhoneNumber());
        if (UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(sysUser_q)))
            return AjaxResult.error("该手机号已被注册");
        userImportService.createWorkerUser(workerRegisterVo, enterprise.getId(), operName);
        return AjaxResult.success("成功注册，登录用户名：w" + workerRegisterVo.getPhoneNumber());
    }
}
